import React, { Component } from 'react';
import './index.less';
import { base } from 'nc-lightapp-front';

const { NCModal, NCButton } = base;
const { Header: NCModalHeader, Body: NCModalBody, Footer: NCModalFooter } = NCModal;

class AlertModal extends Component {
	constructor(props) {
		super(props);
		this.state = {
			visible: false,
			message: 'confirm'
		};
		this.show = this.show.bind(this);
		this.handleNext = this.handleNext.bind(this);
		this.beSure = this.beSure.bind(this);
		this.beDeny = this.beDeny.bind(this);
	}

	show(confirmMsg, warningMsg, callback, denyCallback) {
		if ((!confirmMsg && !warningMsg) || (!confirmMsg.length && !warningMsg.length)) {
			callback && callback();
			return;
		}
		this.confirmMsg = confirmMsg;
		this.warningMsg = warningMsg;
		this.message = '';
		this.callback = callback;
		this.denyCallback = denyCallback;
		this.setState(
			{
				type: 'confirm'
			},
			this.handleNext
		);
	}

	handleNext() {
		const { type } = this.state;
		if (type === 'confirm') {
			if (!this.confirmMsg || !this.confirmMsg[0]) {
				if (!this.warningMsg || !this.warningMsg[0]) {
					this.setState({
						visible: false
					});
					this.callback && this.callback();
				} else {
					this.message = this.warningMsg.splice(0, 1);
					this.setState({
						type: 'warning',
						visible: true
					});
				}
			} else {
				this.message = this.confirmMsg.splice(0, 1);
				this.setState({
					visible: true
				});
			}
		} else {
			if (!this.warningMsg || !this.warningMsg[0]) {
				this.setState({
					visible: false
				});
				this.callback && this.callback();
			} else {
				this.message = this.warningMsg.splice(0, 1);
				this.setState({
					visible: true
				});
			}
		}
	}

	beSure() {
		this.handleNext();
	}

	beDeny() {
		this.setState({
			visible: false
		});
		this.denyCallback && this.denyCallback();
	}

	render() {
		const { visible, type } = this.state;
		const { json } = this.props;
		return (
			<NCModal show={visible} size="sm" backdrop={'static'}>
				<NCModalHeader>
					{type === 'confirm' ? json['cm6011-000056'] : json['cm6011-000030']}
					{/* 国际化处理： 确认,警告,确认*/}
				</NCModalHeader>
				<NCModalBody>
					<div
						dangerouslySetInnerHTML={{
							__html: this.message
						}}
					/>
				</NCModalBody>
				<NCModalFooter>
					<NCButton colors="primary" onClick={this.beSure}>
						{type === 'confirm' ? json['cm6011-000057'] : json['cm6011-000056']}
						{/* 国际化处理： 是,确认,是,确认*/}
					</NCButton>
					{type === 'confirm' ? (
						<NCButton shape="border" onClick={this.beDeny}>
							{json['cm6011-000058']}
							{/* 国际化处理： 否*/}
						</NCButton>
					) : null}
				</NCModalFooter>
			</NCModal>
		);
	}
}

export default AlertModal;
